/*
 * zxt.scroller.js
 *
 * Copyright (c) 2010 CUPDATA
 *
 * version 1.0.0
 * history:	hanqiuming 2010-01-08 created
 */

/**
 * <p>翻页器的基础类.</p>
 * @param {int} size 翻页记录数
 * @class 
 */
zxt.ui.ScrollerBase = function(size) {
	zxt.ui.Widget.call(this);
	
	this._total = 0;
	this._size = size || 10;//默认取10条数据
	this._pageIdx = 1;

	this._$IMG_FIRST;
	this._$IMG_PRE;
	this._$IMG_NEXT;
	this._$IMG_LAST;

	this._listerners = [];
};

zxt.defineClazz(zxt.ui.ScrollerBase, zxt.ui.Widget);

/**
 * <p>返回控件类型</p>
 * @function 
 * @type String
 * @return "scholler"
 */
zxt.ui.ScrollerBase.prototype.getType = function() {
	return "zxt.ui.ScrollerBase";
};

/**
 * <p>添加监听器</p>
 * @function
 * @param {Lister} l 监听事件
 * 
 */
zxt.ui.ScrollerBase.prototype.addListener = function(l) {
	this._listerners.push(l);
};

/**
 * <p>告知器</p>
 * @function
 * 
 */
zxt.ui.ScrollerBase.prototype.notify = function() {
	var start = (this._pageIdx - 1) * this._size;
	var limit = this.limit();
	for ( var j=0; j<this._listerners.length; j++) {
		var l = this._listerners[j];
		l.scrollTo(start, limit);//调用MODEL的scrollTo方法
	}
};

/**
 * <p>计算总页数</p>
 * @function
 * 
 */
zxt.ui.ScrollerBase.prototype.pageCount = function() {
	if (this._total == -1)
		return -1;

	// if (this._total==0) return 1;
	if (this._total == 0)
		return 0;

	if (this._total % this._size == 0) {
		/* lizz */
		// return Math.floor(this._total/this._size);
		return this._total / this._size;
	}
	return Math.floor(this._total / this._size) + 1;
};

/**
 * <p>跳转到指定页</p>
 * @function
 * @param {int} idx 选定跳转页号
 * 
 */
zxt.ui.ScrollerBase.prototype.jumpTo = function(idx) {
	this._pageIdx = idx;
};

/**
 * <p>数据的起始位置偏移</p>
 * @function
 * @returns {int} 偏移
 *  
 */
zxt.ui.ScrollerBase.prototype.offset = function() {
	return this._pageIdx * this._size;
};

/**
 * <p>每页显示的记录条数</p>
 * @function
 * @return {int} 每页显示的记录条数
 */
zxt.ui.ScrollerBase.prototype.limit = function() {
	if (this._total == -1) {
		return this._size;
	}
	if ((this.pageCount() >= this._pageIdx)) {
		return this._size;
	}
	return this._total % this._size;
};

/**
 * <p>翻转至第一页</p>
 * @function
 *  
 */
zxt.ui.ScrollerBase.prototype.first = function() {
	if (this._pageIdx == 1) {
		return;
	}
	this._pageIdx = 1;
	this.refresh();
};

/**
 * <p>
 * </p>
 * 
 * @function
 * @description 翻转至前一页
 */
zxt.ui.ScrollerBase.prototype.pre = function() {
	if (this._pageIdx == 1) {
		return;
	}
	this._pageIdx--;
	this.refresh();
};

/**
 * <p>翻转至下一页</p>
 * @function
 * 
 */
zxt.ui.ScrollerBase.prototype.next = function() {
	if (this._pageIdx == this.pageCount()) {
		return;
	}
	this._pageIdx++;
	this.refresh();
};

/**
 *  <p>翻转至最后一页</p>
 * @function
 *
 */
zxt.ui.ScrollerBase.prototype.last = function() {
	if (this._pageIdx == this.pageCount()) {
		return;
	}
	this._pageIdx = this.pageCount();
	this.refresh();
};

/**
 * <p>刷新</p>
 * @function
 * 
 */
zxt.ui.ScrollerBase.prototype.refresh = function(notify) {
	this.renderInner();
	if ( notify === undefined ) notify = true;
	if ( notify )	this.notify();
};

/**
 * <p>Scroller，继承自zxt.ui.ScrollerBase</p>
 * @class Represents a ZxtScroller.
 * @param {int} size 翻页记录数
 */
zxt.ui.Scroller = function(size) {
	zxt.ui.ScrollerBase.call(this, size);

	this._$navPanel = new zxt.ui.TablePanel(1, 6);

	this._$navPanelDom = this._$navPanel.render();
};

zxt.defineClazz(zxt.ui.Scroller, zxt.ui.ScrollerBase);

/**
 * <p>返回控件类型</p>
 * @function 
 * @return {String} "zxt.ui.Scroller"
 */
zxt.ui.Scroller.prototype.getType = function() {
	return "zxt.ui.Scroller";
};

/**
 * <p>渲染翻页器</p>
 * @function
 * @description renderInner
 */
zxt.ui.Scroller.prototype.renderInner = function() {
	var host = this;
	
	var tP = this.pageCount();
	var status = [ true, true, true, true ];
	if (tP == -1) {
		$(this._$navPanel.getCell(0, 0)).text("第" + this._pageIdx + "页");
		var status = [ this._pageIdx <= 1, this._pageIdx <= 1,	false, true ];
	} else {
		$(this._$navPanel.getCell(0, 0)).text(	"第" + this._pageIdx + "页 / 共" + tP + "页, " + this._total + "条");
		status = [ this._pageIdx <= 1, this._pageIdx <= 1,	this._pageIdx >= tP, this._pageIdx >= tP ];
	}
	this._$IMG_FIRST.attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_first"  + (status[0] ? "_disabled" : "") + ".gif");
	this._$IMG_PRE.attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_previous"	+ (status[1] ? "_disabled" : "") + ".gif");
	this._$IMG_NEXT.attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_next"		+ (status[2] ? "_disabled" : "") + ".gif");
	this._$IMG_LAST.attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_last"		+ (status[3] ? "_disabled" : "") + ".gif");
	
	//当鼠标指向翻页控件时，鼠标指针自动变成小手样式，当鼠标指向翻页控件时，出现  下一页/上一页/首页/尾页 的提示
	this._$IMG_FIRST.mouseover(function(){this.style.cursor='pointer'});
	this._$IMG_FIRST.attr("title","首页");
	this._$IMG_PRE.mouseover(function(){this.style.cursor='pointer'});
	this._$IMG_PRE.attr("title","上一页");
	this._$IMG_NEXT.mouseover(function(){this.style.cursor='pointer'});
	this._$IMG_NEXT.attr("title","下一页");
	this._$IMG_LAST.mouseover(function(){this.style.cursor='pointer'});
	this._$IMG_LAST.attr("title","尾页");
	
	this._$IMG_FIRST.attr("width", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_FIRST.attr("height", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_PRE.attr("width", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_PRE.attr("height", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_NEXT.attr("width", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_NEXT.attr("height", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_LAST.attr("width", zxt.cfg.scroller.arrow_side_length);
	this._$IMG_LAST.attr("height", zxt.cfg.scroller.arrow_side_length);
	
	$(this._$navPanel.getCell(0, 1)).unbind();
	$(this._$navPanel.getCell(0, 2)).unbind();
	$(this._$navPanel.getCell(0, 3)).unbind();
	$(this._$navPanel.getCell(0, 4)).unbind();
	
	if ( !status[0] ) $(this._$navPanel.getCell(0, 1)).click(function() {host.first();});	
	if ( !status[1] ) $(this._$navPanel.getCell(0, 2)).click(function() {host.pre();});
	if ( !status[2] ) $(this._$navPanel.getCell(0, 3)).click(function() {host.next();});
	if ( !status[3] ) $(this._$navPanel.getCell(0, 4)).click(function() {host.last();	});
};

/**
 * <p>渲染翻页器</p>
 * @function
 * @return {DOM} 翻页器的DOM节点
 */
zxt.ui.Scroller.prototype.renderSelf = function() {
	this._$IMG_FIRST = $("<img>");
	this._$IMG_PRE = $("<img>");
	this._$IMG_NEXT = $("<img>");
	this._$IMG_LAST = $("<img>");

	$(this._$navPanel.getCell(0, 1)).html(this._$IMG_FIRST);
	$(this._$navPanel.getCell(0, 2)).html(this._$IMG_PRE);
	$(this._$navPanel.getCell(0, 3)).html(this._$IMG_NEXT);
	$(this._$navPanel.getCell(0, 4)).html(this._$IMG_LAST);
	$(this._$navPanel.getCell(0, 5)).width("100%");

	this.renderInner();
	return this._$navPanelDom;
};

/**
 * <p>zxt.ui.DataScroller，继承自zxt.ui.Scroller</p>
 * @class Represents a ZxtModelScroller.
 * @model {zxt.data.TableModel} 列表数据模型
 * @param {int} page 翻页记录书
 * @author qiuming han
 */
zxt.ui.DataScroller = function(model, page) {
	zxt.ui.Scroller.call(this, page);
	
	this.tableModel = model;
	this._total = this.tableModel.count();
	this.tableModel.addListener(this);//tableModel添加监听事件
};



/**
 * <p>解析zxt.ui.DataScroller控件</p>
 * @function
 * @param {DOM} elem DOM节点
 * 
 */
zxt.ui.DataScroller.parse = function(elem) {
	var j = $(elem);

	var _modelStr = j.attr("zxt.model");
	var _model = window[_modelStr];

	var _pageSizeStr = j.attr("zxt.page");
	var _pageSize = parseInt(_pageSizeStr);

	var _instance = new zxt.ui.DataScroller(_model, _pageSize);
	return _instance;
};

zxt.defineClazz(zxt.ui.DataScroller, zxt.ui.Scroller);

/**
 * <p>返回控件类型</p>
 * @function  
 * 
 * @return {String} "zxt.ui.DataScroller"
 */
zxt.ui.DataScroller.prototype.getType = function() {
	return "zxt.ui.DataScroller";
};

/**
 * <p>事件接收器</p>
 * @function
 * @param {Event} e 事件
 * @param {Object} param 参数
 */
// todo param not used
zxt.ui.DataScroller.prototype.accept = function(e, param) {
	switch (e) {
	case zxt.data.TableModelEvents.COT:
		this._total = this.tableModel.count();
		this.renderInner();
		break;
	case zxt.data.TableModelEvents.NEW:
	case zxt.data.TableModelEvents.DEL:
		this._total = this.tableModel.count();
		this.renderInner();
		break;
	case zxt.data.TableModelEvents.PRM:
		this._total = this.tableModel.count();
		this._pageIdx = 1;
		this.renderInner();
		break;
	}
};

/**
 * <p>zxt.ui.Pager,继承自zxt.ui.ScrollerBase</p>
 * 
 * @class Represents a ZxtPager.
 * @param {int} s 
 * @author zzli
 */
zxt.ui.Pager = function(s) {
	zxt.ui.ScrollerBase.call(this);
	
	this._total = 0;
	this._size = s || 10;
	this._pageIdx = 1;

	this.optionCounts = [ 5, 10, 20, 50, 100 ];//每页显示的记录数选择

	this._$IMG_FIRST;
	this._$IMG_PRE;
	this._$IMG_NEXT;
	this._$IMG_LAST;

	this._$navPanel = new zxt.ui.TablePanel(1, 18);
	this._$navPanelDom = this._$navPanel.render();

	this._$holderPanel = new zxt.ui.TablePanel(1, 2);
	this._$holderPanelDom = this._$holderPanel.render();

	$(this._$holderPanel.getCell(0, 0)).width("100%");
	$(this._$holderPanel.getCell(0, 1)).append(this._$navPanelDom);

	this._listerners = [];
};

zxt.defineClazz(zxt.ui.Pager, zxt.ui.ScrollerBase);

/**
 *  <p>返回控件类型</p>
 * @function *
 *
 * @return {String} "zxt.ui.Pager"
 */
zxt.ui.Pager.prototype.getType = function() {
	return "zxt.ui.Pager";
};

/**
 * <p>每页显示数改变</p>
 * @function 
 * @param {int} sc sizechange
 */
zxt.ui.Pager.prototype.sizeChange = function(sc) {
	this._pageIdx = 1;
	this._size = sc;
	this.sizeSelect.val(sc);
	this.refresh();
};

/**
 * <p>当前选择页改变</p>
 * @function 
 * 
 * @param pc pagechange
 */
zxt.ui.Pager.prototype.pageChange = function(pc) {
	this._pageIdx = pc;
	this.refresh();
};

/**
 * <p>渲染翻页器</p>
 * @function
 * 
 */
zxt.ui.Pager.prototype.renderInner = function() {
	var tP = this.pageCount();// 总页数
	var pIdx = this._pageIdx;// 当前页数
	var _disabled = [ true, true, true, true ];

	if (tP != -1) {
		var _disabled = [ pIdx <= 1, pIdx <= 1, pIdx >= tP, pIdx >= tP ];
	};
	this._$IMG_FIRST.attr("_pos", "first").attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_first"	+ (_disabled[0] ? "_disabled" : "") + ".gif");
	this._$IMG_PRE.attr("_pos", "pre").attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_previous" + (_disabled[1] ? "_disabled" : "") + ".gif");
	this._$IMG_NEXT.attr("_pos", "next").attr("src",zxt.cfg.IMAGES_PATH + "/efgrid_page_next" + (_disabled[2] ? "_disabled" : "") + ".gif");
	this._$IMG_LAST.attr("_pos", "last").attr("src", zxt.cfg.IMAGES_PATH + "/efgrid_page_last" + (_disabled[3] ? "_disabled" : "") + ".gif");

	$(this.pageSelect).empty();
	
	for ( var i = 0; i < tP; i++) {
		var v = i + 1;
		this.pageSelect.append($("<option value=" + v + " />").text(v + ""));
	};
	
	this.pageSelect.attr("value", this._pageIdx);
	$(this._$navPanel.getCell(0, 12)).append(this.pageSelect);
	$(this._$navPanel.getCell(0, 14)).empty().append(tP);
};

/**
 *  <p>渲染翻页器</p>
 * @function
 * 
 */
zxt.ui.Pager.prototype.renderSelf = function() {
	var scrollerT = this;
	var _divider = "<IMG SRC=\"" + zxt.cfg.IMAGES_PATH
			+ "/efgrid_blue_divider.gif\" >";
	this._$IMG_FIRST = $("<img>");
	this._$IMG_PRE = $("<img>");
	this._$IMG_NEXT = $("<img>");
	this._$IMG_LAST = $("<img>");

	$(this._$navPanel.getCell(0, 0)).attr("nowrap", "");
	$(this._$navPanel.getCell(0, 1)).append($(_divider));

	$(this._$navPanel.getCell(0, 2)).append(
			$("<div/>").css("text-align", "center").width("20px").append(this._$IMG_FIRST)).attr("title", "首页")
				.addClass("por-Button").width("20px").css("cursor", "pointer")
				.click(function() {	scrollerT.first(); });

	$(this._$navPanel.getCell(0, 3)).append(
			$("<div/>").css("text-align", "center").width("20px").append(this._$IMG_PRE)).attr("title", "上一页").addClass("por-Button")
				.width("20px").css("cursor", "pointer")
				.click(function() {	scrollerT.pre();});

	$(this._$navPanel.getCell(0, 4)).append(
			$("<div/>").css("text-align", "center").width("20px").append(this._$IMG_NEXT)).attr("title", "下一页")
				.addClass("por-Button").width("20px").css("cursor", "pointer")
				.click(function() {scrollerT.next();});
				
	$(this._$navPanel.getCell(0, 5)).append(
			$("<div/>").css("text-align", "center").width("20px").append(this._$IMG_LAST)).attr("title", "末页").addClass("por-Button")
				.width("20px").css("cursor", "pointer")
				.click(function() {	scrollerT.last(); });

	$(this._$navPanel.getCell(0, 6)).append($(_divider));
	$(this._$navPanel.getCell(0, 7)).attr("nowrap", "").text("每页");

	this.sizeSelect = $("<select style='font-size: 12px;background-color: #fcfdfd; border: 1px solid #d3d5d5;'/>");//每页显示记录数
	for ( var i = 0; i < this.optionCounts.length; i++) {
		var v = this.optionCounts[i];
		this.sizeSelect.append($("<option value=" + v + " />").text(v + ""));
	}
	
	$(this._$navPanel.getCell(0, 8)).append(this.sizeSelect);
	$(this._$navPanel.getCell(0, 9)).attr("nowrap", "").text("条");

	var host = this;
	this.sizeSelect.change(function() {
		var num = parseInt(this.value);
		host.sizeChange(num);
	});
	this.sizeSelect.get(0).value = this._size;

	$(this._$navPanel.getCell(0, 10)).append($(_divider));

	$(this._$navPanel.getCell(0, 11)).attr("nowrap", "").text("跳转到第");

	
	this.pageSelect = $("<select style='font-size: 12px;background-color: #fcfdfd; border: 1px solid #d3d5d5;'/>");
	this.pageSelect.change(function() {
		var idx = this.value;
		host.pageChange(idx);
	});

	$(this._$navPanel.getCell(0, 13)).attr("nowrap", "").text("页/共");
	$(this._$navPanel.getCell(0, 15)).attr("nowrap", "").text("页");

	this.goBtn = $("<input type=button value=GO>").addClass("por-Button").attr(	"title", "跳转");
	//$(this._$navPanel.getCell(0, 16)).append(this.goBtn);
	//$(this._$navPanel.getCell(0, 17)).append($(_divider));

	this.renderInner();
	return this._$holderPanelDom;
};

/**
 * <p>zxt.ui.DataPager,继承自zxt.ui.Pager</p>
 * 
 * @class Represents a DataPager.
 * @model {zxt.data.TableModel} 列表数据模型
 * @author zzli
 */
zxt.ui.DataPager = function(model) {
	zxt.ui.Pager.call(this);
	
	this.tableModel = model;
	this._total = this.tableModel.count();//取得全部记录数，以计算总页数
	this.tableModel.addListener(this);

};

/**
 * <p>解析zxt.ui.DataPager控件</p>
 * @function
 * @param {DOM} elem DOM节点
 * 
 */
zxt.ui.DataPager.parse = function(elem) {
	var j = $(elem);

	var _modelStr = j.attr("zxt.model");
	var _model = window[_modelStr];

	var _instance = new zxt.ui.DataPager(_model);
	return _instance;
};

zxt.defineClazz(zxt.ui.DataPager, zxt.ui.Pager);

/**
 * <p>返回控件类型</p>
 * @function
 *  
 * @return {String} "zxt.ui.DataPager"
 */
zxt.ui.DataPager.prototype.getType = function() {
	return "zxt.ui.DataPager";
};

/**
 * <p>事件接收器</p>
 * @function * 
 * @param {Evnet} e 事件
 * @param {Object} param 参数 
 * @throws
 */
// Todo param not used
zxt.ui.DataPager.prototype.accept = function(e, param) {
	switch (e) {
		case zxt.data.TableModelEvents.COT:
			this._total = this.tableModel.count();
			this.renderInner();
			break;
		case zxt.data.TableModelEvents.NEW:
		case zxt.data.TableModelEvents.DEL:
			this._total = this.tableModel.count();
			this.refresh();
			break;
		case zxt.data.TableModelEvents.PRM:
			this._total = this.tableModel.count();
			this._pageIdx = 1;
			this.refresh();
			break;
	}
};